package com.tools.cleanmaster.functionpage;

import android.content.Context;
import android.os.Message;
import android.text.Html;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.hm.base.android.ui.AActivity;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;
import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.R;
import com.tools.cleanmaster.store.GlobalConfigSharedPrefManager;
import com.tools.cleanmaster.ui.card.animator.AnimatorListenerAdapter;
import com.tools.cleanmaster.utils.FeatureConfig;
import com.tools.cleanmaster.utils.LogHelper;
import com.tools.cleanmaster.utils.StringUtils;

import java.util.ArrayList;


public class ScanAnimationManager {

    private View mViewRoot;
    private ImageView rippleRing;
    private ImageView rippleRing1;
    private ImageView mHomeRingLogo;
    private ImageView imageViewShape;
    private FrameLayout scanViewLayout;
    private AnimatorSet rippleSet;
    private AnimatorSet pulseSet;
    private TextView state;
    private TextView stateDesc;
    private Context mContext;

    public ScanAnimationManager(View viewRoot, Context context, CenterCircleClick dangerCircleClick) {
        if ((dangerCircleClick == null || viewRoot == null) && FeatureConfig.DEBUG_LOG) {
            throw new IllegalArgumentException("viewRoot should not null!!!");
        }
        this.mContext = context;
        this.centerCircleClick = dangerCircleClick;
        initView(viewRoot);
    }

    public interface AnimationListener {
        void onAnimationEnd();
    }

    private void initView(View viewRoot) {
        this.mViewRoot = viewRoot;
        this.rippleRing = viewRoot.findViewById(R.id.ripple);
        this.rippleRing1 = viewRoot.findViewById(R.id.ripple1);
        this.mHomeRingLogo = viewRoot.findViewById(R.id.home_ring_logo);
        this.scanViewLayout = viewRoot.findViewById(R.id.scan_result_layout);
        this.imageViewShape = viewRoot.findViewById(R.id.image_view_ring_shape);
        this.state = viewRoot.findViewById(R.id.state);
        this.stateDesc = viewRoot.findViewById(R.id.state_description);

        if(this.mContext instanceof AActivity){
            Message message = Message.obtain();
            message.what = R.id.msg_reset_function_icon_state;
            ((AActivity) this.mContext).subHandleMessage(message);
        }
    }

    private static final long ONE_RIPPLE_TIME = 2000L;

    private void playRippleAnimation() {
        if (rippleSet == null) {
            rippleSet = new AnimatorSet();
        } else {
            if (rippleSet.isRunning()) {
                rippleSet.cancel();
            }
        }

        ObjectAnimator alpha = ObjectAnimator.ofFloat(rippleRing, "alpha", 0.1f, 0.4f, 0.3f, 0.1f, 0f);
        alpha.setRepeatMode(Animation.RESTART);
        alpha.setRepeatCount(Animation.INFINITE);
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(rippleRing, "scaleX", 1f, 1.2f);
        scaleX.setRepeatMode(Animation.RESTART);
        scaleX.setRepeatCount(Animation.INFINITE);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(rippleRing, "scaleY", 1f, 1.2f);
        scaleY.setRepeatMode(Animation.RESTART);
        scaleY.setRepeatCount(Animation.INFINITE);
        scaleY.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                rippleRing.setVisibility(View.VISIBLE);
            }
        });

        ObjectAnimator alpha1 = ObjectAnimator.ofFloat(rippleRing1, "alpha", 0.6f, 0.3f, 0.1f, 0f);
        alpha1.setRepeatMode(Animation.RESTART);
        alpha1.setRepeatCount(Animation.INFINITE);
        alpha1.setStartDelay(1000L);
        ObjectAnimator scaleX1 = ObjectAnimator.ofFloat(rippleRing1, "scaleX", 1f, 1.2f);
        scaleX1.setRepeatMode(Animation.RESTART);
        scaleX1.setRepeatCount(Animation.INFINITE);
        scaleX1.setStartDelay(1000L);
        ObjectAnimator scaleY1 = ObjectAnimator.ofFloat(rippleRing1, "scaleY", 1f, 1.2f);
        scaleY1.setRepeatMode(Animation.RESTART);
        scaleY1.setRepeatCount(Animation.INFINITE);
        scaleY1.setStartDelay(1000L);
        scaleY1.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                rippleRing1.setVisibility(View.VISIBLE);
            }
        });

        rippleSet.playTogether(alpha, scaleX, scaleY, alpha1, scaleX1, scaleY1);
        rippleSet.setInterpolator(new DecelerateInterpolator());
        rippleSet.setDuration(ONE_RIPPLE_TIME);
        rippleSet.start();
    }

    private void playPulseAnimation() {
        if (pulseSet == null) {
            pulseSet = new AnimatorSet();
        } else {
            if (pulseSet.isRunning()) {
                pulseSet.cancel();
            }
        }

        ObjectAnimator scaleX = ObjectAnimator.ofFloat(scanViewLayout, "scaleX", 1f, 1.05f, 1f, 1.04f, 1f, 1f, 1f, 1f, 1f, 1f, 1f);
        scaleX.setRepeatMode(Animation.RESTART);
        scaleX.setRepeatCount(Animation.INFINITE);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(scanViewLayout, "scaleY", 1f, 1.05f, 1f, 1.04f, 1f, 1f, 1f, 1f, 1f, 1f, 1f);
        scaleY.setRepeatMode(Animation.RESTART);
        scaleY.setRepeatCount(Animation.INFINITE);

        pulseSet.playTogether(scaleX, scaleY);
        pulseSet.setInterpolator(new DecelerateInterpolator());
        pulseSet.setDuration(ONE_RIPPLE_TIME * 2);
        pulseSet.start();
    }

    private void playOutAnimation() {
        final AnimatorSet outSet = new AnimatorSet();
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(scanViewLayout, "scaleX", 1f, 0.5f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(scanViewLayout, "scaleY", 1f, 0.5f);
        ObjectAnimator alpha1 = ObjectAnimator.ofFloat(scanViewLayout, "alpha", 1.0f, 0f);
        outSet.playTogether(scaleX, scaleY, alpha1);
        outSet.setInterpolator(new AccelerateDecelerateInterpolator());
        outSet.setDuration(500L);
        outSet.addListener(new com.nineoldandroids.animation.AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                mViewRoot.findViewById(R.id.home_page_scan).setVisibility(View.GONE);
            }
        });
        outSet.start();
    }

    public void startAnimation(boolean isShowAdunlock) {
        scanViewLayout.setVisibility(View.VISIBLE);
        scanViewLayout.setAlpha(1.0f);
        mViewRoot.setVisibility(View.VISIBLE);

        mViewRoot.findViewById(R.id.silent_state_silent).setVisibility(View.VISIBLE);
        mViewRoot.findViewById(R.id.state).setVisibility(View.VISIBLE);
        imageViewShape.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (centerCircleClick != null) {
                    centerCircleClick.click();
                }
            }
        });
        playRippleAnimation();
        playPulseAnimation();
    }

    private CenterCircleClick centerCircleClick;

    public void releaseResources() {
        if (rippleSet != null) {
            if (rippleSet.isRunning()) {
                ArrayList<Animator> childAnimation = rippleSet.getChildAnimations();
                for (Animator animator : childAnimation) {
                    if (animator.isRunning()) {
                        animator.cancel();
                    }
                }
                rippleSet.end();
                rippleSet.cancel();
            }
            rippleSet = null;
            LogHelper.d("memory", "rippleSet release");
        }
        if (pulseSet != null) {
            if (pulseSet.isRunning()) {
                ArrayList<Animator> childAnimation = pulseSet.getChildAnimations();
                for (Animator animator : childAnimation) {
                    if (animator.isRunning()) {
                        animator.cancel();
                    }
                }
                pulseSet.end();
                pulseSet.cancel();
            }
            pulseSet = null;
            LogHelper.d("memory", "pulseSet release");
        }
        playOutAnimation();
    }

    public void setState(FunctionPageState mState) {
        if (mState == FunctionPageState.BLUE) {
            state.setText(R.string.first_page_scan_title);
            state.setTextColor(mContext.getResources().getColor(R.color.white));
            stateDesc.setText(R.string.system_trash_item);
            stateDesc.setTextColor(mContext.getResources().getColor(R.color.white));
            mHomeRingLogo.setImageResource(R.drawable.home_ring_besom);

            rippleRing.setBackgroundResource(R.drawable.ring_dot_star);
            rippleRing1.setBackgroundResource(R.drawable.ring_dot_star);

        } else {
            long size = GlobalConfigSharedPrefManager.getLeftOverSize(CleanApplication.getInstance());
            state.setText(R.string.cleanmaster_notification_button);
            state.setTextColor(mContext.getResources().getColor(R.color.white));
            stateDesc.setText(Html.fromHtml(mContext.getString(R.string.first_page_scan_content, StringUtils.getSizeString(size))));
            stateDesc.setTextColor(mContext.getResources().getColor(R.color.white));
            mHomeRingLogo.setImageResource(R.drawable.home_ring_garbage);

            rippleRing.setBackgroundResource(R.drawable.ring_dot_dust);
            rippleRing1.setBackgroundResource(R.drawable.ring_dot_dust);
        }

        ((TextView) mViewRoot.findViewById(R.id.stats_notification)).setText(Html.fromHtml(mContext.getString(R.string.first_page_scan_notification, StringUtils
                .getSizeString(GlobalConfigSharedPrefManager.getTrashCleanTotal(CleanApplication.getInstance())))));
    }
}
